﻿using CommonTools;
using FSUIPC;
using SimWithP3d.Vehicle;
using LockheedMartin.Prepar3D.SimConnect;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Threading;
using System.Windows.Controls;
using SimWithP3d.ViewModel;

namespace SimWithP3d
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        MainViewModel mainViewModel;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            mainViewModel = new MainViewModel();
            this.DataContext = mainViewModel;
        }
        private void Window_Closed(object sender, EventArgs e)
        {
            mainViewModel.Dispose();
        }
        /// <summary>
        /// 保存校准数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Save_Click(object sender, RoutedEventArgs e)
        {
            int area = 0x02;
            int mark = 60002;
            byte[] dataX = new byte[6];
            switch (mainViewModel.CurrenSelectedIndex)
            {
                case 0:
                    dataX[2] = 0x40;
                    break;
                case 1:
                    dataX[2] = 0x40;
                    break;
                case 2:
                    dataX[2] = 0x20;
                    break;
                case 3:
                    dataX[2] = 0x10;
                    break;
                case 4:
                    dataX[2] = 0x10;
                    break;
                case 5:
                    dataX[2] = 0x10;
                    break;
                case 6:
                    dataX[2] = 0x10;
                    break;
                case 7:
                    area = 0x01;
                    mark = 60001;
                    dataX[1] = 0x80;
                    break;
                case 8:
                    area = 0x01;
                    mark = 60001;
                    dataX[1] = 0x40;
                    break;
                case 9:
                    area = 0x01;
                    mark = 60001;
                    dataX[1] = 0x20;
                    break;
                case 10:
                    area = 0x01;
                    mark = 60001;
                    dataX[1] = 0x20;
                    break;
                case 11:
                    area = 0x01;
                    mark = 60001;
                    dataX[1] = 0x10;
                    break;
                default:
                    break;
            }
            App.vehicleDevice.SendData(area, mark, dataX);
        }
        /// <summary>
        /// 最小值 校准
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Min_Click(object sender, RoutedEventArgs e)
        {
            int area = 0x02;
            int mark = 60002;
            byte[] dataX = new byte[6];
            switch (mainViewModel.CurrenSelectedIndex)
            {
                case 0:
                    dataX[0] = 0x80;
                    break;
                case 1:
                    dataX[0] = 0x10;
                    break;
                case 2:
                    dataX[0] = 0x02;
                    break;
                case 3://主左脚踏自然归零
                    dataX[1] = 0x40;
                    break;
                case 4://主右脚踏自然归零
                    dataX[1] = 0x10;
                    break;
                case 5://副左脚踏自然归零
                    dataX[1] = 0x04;
                    break;
                case 6://副右脚踏自然归零
                    dataX[1] = 0x01;
                    break;
                case 7:
                    area = 0x01;
                    mark = 60001;
                    dataX[0] = 0x80;
                    break;
                case 8:
                    area = 0x01;
                    mark = 60001;
                    dataX[0] = 0x20;
                    break;
                case 9:
                    area = 0x01;
                    mark = 60001;
                    dataX[0] = 0x08;
                    break;
                case 10:
                    area = 0x01;
                    mark = 60001;
                    dataX[0] = 0x02;
                    break;
                case 11:
                    area = 0x01;
                    mark = 60002;
                    dataX[0] = 0x01;
                    byte[] value = App.vehicleDevice.IntToHexByte(mainViewModel.Indicator_min);
                    dataX[1] = value[0];
                    dataX[2] = value[1];
                    break;
                default:
                    break;
            }
            App.vehicleDevice.SendData(area, mark, dataX);
        }
        /// <summary>
        /// 中值校准
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Middel_Click(object sender, RoutedEventArgs e)
        {
            int area = 0x02;
            int mark = 60002;
            byte[] dataX = new byte[6];
            switch (mainViewModel.CurrenSelectedIndex)
            {
                case 0:
                    dataX[0] = 0x40;
                    break;
                case 1:
                    dataX[0] = 0x08;
                    break;
                case 2:
                    dataX[0] = 0x01;
                    break;
                default:
                    break;
            }
            App.vehicleDevice.SendData(area, mark, dataX);
        }
        /// <summary>
        /// 最大值校准
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Max_Click(object sender, RoutedEventArgs e)
        {
            int area = 0x02;
            int mark = 60002;
            byte[] dataX = new byte[6];
            switch (mainViewModel.CurrenSelectedIndex)
            {
                case 0:
                    dataX[0] = 0x20;
                    break;
                case 1:
                    dataX[0] = 0x04;
                    break;
                case 2:
                    dataX[1] = 0x80;
                    break;
                case 3://主左脚踏踩到底                   
                    dataX[1] = 0x20;
                    break;
                case 4://主右脚踏踩到底
                    dataX[1] = 0x08;
                    break;
                case 5://副左脚踏踩到底
                    dataX[1] = 0x02;
                    break;
                case 6://副右踏板最大数据
                    dataX[2] = 0x80;
                    break;
                case 7:
                    area = 0x01;
                    mark = 60001;
                    dataX[0] = 0x40;
                    break;
                case 8:
                    area = 0x01;
                    mark = 60001;
                    dataX[0] = 0x10;
                    break;
                case 9:
                    area = 0x01;
                    mark = 60001;
                    dataX[0] = 0x04;
                    break;
                case 10:
                    area = 0x01;
                    mark = 60001;
                    dataX[0] = 0x01;
                    break;
                case 11:
                    area = 0x01;
                    mark = 60002;
                    dataX[0] = 0x02;
                    byte[] value = App.vehicleDevice.IntToHexByte(mainViewModel.Indicator_max);
                    dataX[1] = value[0];
                    dataX[2] = value[1];
                    break;
                default:
                    break;
            }
            App.vehicleDevice.SendData(area, mark, dataX);
        }
    }
}
